home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Mac-Source 1994 July
/
Mac-Source_July_1994.iso
/
C and C++
/
Miscellaneous
/
ival
/
misc.c
< prev
next >
Wrap
Text File
|
1987-05-25
|
3KB
|
114 lines
/*
* misc.c - miscellaneous routines
*
*/
#include <memory.h>
#include <quickdraw.h>
#include <osutil.h>
#include <resource.h>
/*
* PictToMap() - convert a resource picture into a bitmap containing the
* result of drawing that picture.
* The resulting bitmap's upper-left corner is [0,0].
*/
BitMap *
PictToMap(pic)
PicHandle pic;
{
GrafPtr saveport; /* current port (to be restored) */
GrafPort aport; /* a temp grafport for drawing into */
BitMap *mapptr; /* the result */
short width, height; /* dimensions of the picture */
char *malloc();
LoadResource(pic);
HLock((Handle) pic);
width = (*pic)->picFrame.right - (*pic)->picFrame.left;
height = (*pic)->picFrame.bottom - (*pic)->picFrame.top;
HUnlock((Handle) pic);
mapptr = (BitMap *) malloc(sizeof(BitMap));
SetRect(&mapptr->bounds, 0, 0, width, height);
mapptr->rowBytes = (width + 7) / 8;
if (mapptr->rowBytes & 1) {
++(mapptr->rowBytes);
}
mapptr->baseAddr = (QDPtr) malloc((int) mapptr->rowBytes * height);
GetPort(&saveport);
OpenPort(&aport);
SetPortBits(mapptr);
DrawPicture(pic, &mapptr->bounds);
SetPort(saveport);
ClosePort(&aport);
return(mapptr);
}
/*
* randint() - return a random number in the range 0 through range-1.
* This routine seeds the number generator on the first call.
*/
int
randint(range)
int range;
{
static int first = 1; /* true if this is the first call */
long secs; /* value returned from GetDateTime() */
if (first) {
first = 0;
GetDateTime(&secs);
randSeed = secs;
}
return((int)((unsigned) Random() % range));
}
/*
* setupmemory() - initialize the memory.
*/
setupmemory()
{
#define maxStackSize 8192 /* max size of stack; the heap gets the rest */
typedef long *lomemptr; /* a pointer to low memory locations */
lomemptr stackbaseptr; /* points to current stack base */
/*
If you define a GrowZone function to handle bad memory problems,
you should define it at the top level (not nested), and set it
here. We don't.
*/
/* SetGrowZone(&mygrowzone); */
/*
If you needed to use an Application heap limit other than the
default (which allows 8K for the stack), you'd set it here,
possible using this technique of explicitly specifying the maximum
stack size and allocating the rest to the heap. Should be
independent of memory size.
*/
stackbaseptr = (lomemptr) 0x908; /* CurStackBase from Tlasm/sysequ.text */
SetApplLimit((Ptr) (*stackbaseptr - maxStackSize));
/*
Expand the application heap zone to its maximum size, without
purging any purgeable resources. This saves memory compactions
and heap expansions later.
*/
MaxApplZone();
/*
get plenty of master pointers now; if we let the Memory Manager
allocate them as needed, they'd form non-relocatable islands in
the heap.
*/
MoreMasters();
MoreMasters();
}